#!/bin/bash

# 多服务容器演示脚本
echo "🎯 多服务容器演示"
echo "=================="

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 检查Docker是否运行
if ! docker info > /dev/null 2>&1; then
    echo -e "${RED}❌ Docker未运行，请先启动Docker${NC}"
    exit 1
fi

# 构建镜像
echo -e "${BLUE}🔨 构建多服务容器镜像...${NC}"
docker build -t practice-platform/multi-service:latest .

if [ $? -ne 0 ]; then
    echo -e "${RED}❌ 镜像构建失败${NC}"
    exit 1
fi

echo -e "${GREEN}✅ 镜像构建成功${NC}"

# 启动容器
echo -e "${BLUE}🚀 启动多服务容器...${NC}"
docker-compose up -d

if [ $? -ne 0 ]; then
    echo -e "${RED}❌ 容器启动失败${NC}"
    exit 1
fi

echo -e "${GREEN}✅ 容器启动成功${NC}"

# 等待服务就绪
echo -e "${YELLOW}⏳ 等待服务就绪...${NC}"
sleep 15

# 检查容器状态
echo -e "${BLUE}📊 检查容器状态...${NC}"
docker ps | grep multi-service

# 测试MySQL连接
echo -e "${BLUE}🐬 测试MySQL连接...${NC}"
if mysql -h localhost -P 3306 -u root -p123456 -e "SELECT VERSION();" 2>/dev/null; then
    echo -e "${GREEN}✅ MySQL连接成功${NC}"
else
    echo -e "${RED}❌ MySQL连接失败${NC}"
fi

# 测试Redis连接
echo -e "${BLUE}🔴 测试Redis连接...${NC}"
if redis-cli -h localhost -p 6379 -a 123456 ping 2>/dev/null | grep -q "PONG"; then
    echo -e "${GREEN}✅ Redis连接成功${NC}"
else
    echo -e "${RED}❌ Redis连接失败${NC}"
fi

# 演示数据操作
echo -e "${BLUE}💾 演示数据操作...${NC}"

# MySQL操作演示
echo "  MySQL操作演示："
mysql -h localhost -P 3306 -u root -p123456 -e "
    CREATE DATABASE IF NOT EXISTS demo_db;
    USE demo_db;
    CREATE TABLE IF NOT EXISTS users (
        id INT AUTO_INCREMENT PRIMARY KEY,
        username VARCHAR(50) NOT NULL,
        email VARCHAR(100) NOT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
    INSERT INTO users (username, email) VALUES 
        ('demo_user1', 'user1@example.com'),
        ('demo_user2', 'user2@example.com'),
        ('demo_user3', 'user3@example.com');
    SELECT * FROM users;
" 2>/dev/null

# Redis操作演示
echo "  Redis操作演示："
redis-cli -h localhost -p 6379 -a 123456 << EOF
SET user:1 "demo_user1"
SET user:2 "demo_user2"
SET user:3 "demo_user3"
GET user:1
GET user:2
GET user:3
KEYS user:*
EOF

# 显示资源使用情况
echo -e "${BLUE}📈 资源使用情况...${NC}"
docker stats --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}" | head -2
docker stats --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}" | grep multi-service

# 显示日志
echo -e "${BLUE}📝 最近日志...${NC}"
docker logs --tail 10 multi-service-demo

echo ""
echo -e "${GREEN}🎉 演示完成！${NC}"
echo "=================="
echo ""
echo -e "${YELLOW}📋 可用操作：${NC}"
echo "  查看日志: docker logs -f multi-service-demo"
echo "  进入容器: docker exec -it multi-service-demo bash"
echo "  停止容器: docker-compose down"
echo "  重启服务: docker exec multi-service-demo supervisorctl restart all"
echo ""
echo -e "${YELLOW}🔗 服务连接信息：${NC}"
echo "  MySQL: localhost:3306 (root/123456)"
echo "  Redis: localhost:6379 (密码: 123456)"
echo ""
echo -e "${YELLOW}⚠️  注意事项：${NC}"
echo "  这是一个演示环境，不建议用于生产"
echo "  数据存储在Docker卷中，重启容器数据会保留"
echo "  使用完毕后请运行 'docker-compose down' 清理资源" 